<?php
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', dirname(__FILE__) . DS);

include_once BP . 'lib/Kd/Loader.php';
include_once BP . 'lib/Kd/Autoload.php';
include_once BP . 'pri/code/App.php';

Kd_Loader::addIncludePath(BP . 'lib' . DS);
Kd_Autoload::register();

Kd_Web::getSession()->start();

if (!empty($_FILES)) {
        $files = Kd_Web::getRequest()->getAllFiles();
        $dir = Kd_Web::getRequest()->getParameter('folder', NULL);
        if(is_null($dir)){
        	$dir = DP_TMP.'upload'.DS.Kd_Util_Random::randomNumeric();
        }else{
        	$dir = Kd_Web_Util::urlToPath($dir);
        }
        Kd_Util_IO::makeDirectory($dir);
        $fileUrls = array();
        foreach($files as $file){
        	$fileUrls[] = '/'.str_replace(DS, '/', str_replace(BP, '', $file->saveRandom($dir)));
        }
        echo Kd_Util_Json::encode(array('success'=>1, 'fileUrls'=>$fileUrls));
        exit();
}